<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>返回聚合的类型 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'returning-aggregation-type.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/returning-aggregation-type.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/returning-aggregation-type.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/returning-aggregation-type.html" rel="nofollow" target="_blank">../en/returning-aggregation-type.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-node">返回聚合的类型</span>
</div>
<div class="navheader">
<span class="prev">
<a href="agg-metadata.html">« 聚合元数据</a>
</span>
<span class="next">
<a href="indexing-aggregation-results.html">使用转换对聚合结果进行索引 »</a>
</span>
</div>
<div class="chapter">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="returning-aggregation-type"></a>返回聚合的类型
</h2>
</div></div></div>
<p>
有时需要知道聚合的确切类型，以便解析结果。

参数<code class="literal">typed_keys</code>可用于更改响应中的聚合名称，以便以其内部类型作为前缀。
</p>
<p>
考虑以下名为<code class="literal">tweets_over_time</code>的<a class="xref" href="search-aggregations-bucket-datehistogram-aggregation.html" title="Date histogram aggregation"><code class="literal">日期直方图(date_histogram)</code>聚合</a>，它有一个名为<code class="literal">top_users</code>的子<a class="xref" href="search-aggregations-metrics-top-hits-aggregation.html" title="top_hits聚合">top_hits聚合</a>：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /twitter/_search?typed_keys
{
  "aggregations": {
    "tweets_over_time": {
      "date_histogram": {
        "field": "date",
        "calendar_interval": "year"
      },
      "aggregations": {
        "top_users": {
            "top_hits": {
                "size": 1
            }
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/591.console"></div>
<p>
在响应中，聚合的名称将分别更改为<code class="literal">date_histogram#tweets_over_time</code>和<code class="literal">top_hits#top_users</code>，以反映每个聚合的内部类型：
</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    "aggregations": {
        "date_histogram#tweets_over_time": { <a id="CO279-1"></a><i class="conum" data-value="1"></i>
            "buckets" : [
                {
                    "key_as_string" : "2009-01-01T00:00:00.000Z",
                    "key" : 1230768000000,
                    "doc_count" : 5,
                    "top_hits#top_users" : {  <a id="CO279-2"></a><i class="conum" data-value="2"></i>
                        "hits" : {
                            "total" : {
                                "value": 5,
                                "relation": "eq"
                            },
                            "max_score" : 1.0,
                            "hits" : [
                                {
                                  "_index": "twitter",
                                  "_type": "_doc",
                                  "_id": "0",
                                  "_score": 1.0,
                                  "_source": {
                                    "date": "2009-11-15T14:12:12",
                                    "message": "trying out Elasticsearch",
                                    "user": "kimchy",
                                    "likes": 0
                                  }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    },
    ...
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO279-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>
名称<code class="literal">tweets_over_time</code>现在包含了<code class="literal">date_histogram</code>前缀。
</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO279-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>
名称<code class="literal">top_users</code>现在包含了<code class="literal">top_hits</code>前缀。
</p>
</td>
</tr>
</table>
</div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>
对于某些聚合，返回的类型可能与请求中提供的类型不同。<br/><br/>

terms、significant_terms 和 percentiles(百分位数)聚合就是这种情况，其中返回的类型还包含有关目标字段类型的信息：
<code class="literal">lterms</code>(用于长整形字段上的词项聚合)，
<code class="literal">sigsterms</code>(用于字符串字段上的重要词项聚合)，
<code class="literal">tdigest_percentiles</code>(用于基于 TDigest 算法的百分位数聚合)。
</p>
</div>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="agg-metadata.html">« 聚合元数据</a>
</span>
<span class="next">
<a href="indexing-aggregation-results.html">使用转换对聚合结果进行索引 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>